<?php

class integrate
{
	var $db_host		= '';
	var $db_name		= '';
	var $db_user		= '';
	var $db_pass		= '';
	
	var $prefix			= '';
	
	var $charset		= '';
	var $cookie_domain	= '';
	var $cookie_path	= '/';
	var $user_table		= '';
	
	var $field_id		= '';
	var $field_name		= '';
	var $field_pass		= '';
	var $field_email	= '';
	
	var $field_gender	= '';
	var $field_bday		= '';
	var $field_reg_date	= '';
	var $need_sync 		= false;
	
	var $error			= 0;
	
	var $db;
	
	function integrate($cfg)
	{
		$this->charset = isset($cfg['db_charset']) ? $cfg['db_charset'] : 'UTF8';
		$this->prefix = isset($cfg['prefix']) ? $cfg['prefix'] : '';
		$this->db_name = isset($cfg['db_name']) ? $cfg['db_name'] : '';
		$this->cookie_domain = isset($cfg['cookie_domain']) ? $cfg['cookie_domain'] : '';
		$this->cookie_path = isset($cfg['cookie_path']) ? $cfg['cookie_path'] : '/';
		$this->need_sync = false;
		
		$quiet = empty($cfg['quiet']) ? 0 : 1;
		
		if(empty($cfg['db_host']))
		{
			$this->db_name = $GLOBALS['ecs']->db_name;
			$this->prefix = $GLOBALS['ecs']->prefix;
			$this->db = &$GLOBALS['db'];
		}
		else
		{
			if(empty($cfg['is_latin1']))
			{
				$this->db = new cls_mysql($cfg['db_host'], $cfg['db_user'], $cfg['db_pass'],$cfg['db_name'],$this->charset,NULL,$quiet);
			}
			else 
			{
				$this->db = new cls_mysql($cfg['db_host'], $cfg['db_user'], $cfg['db_pass'],$cfg['db_name'],'latin1',NULL,$quiet);
			}
		}
		
		if(!is_resource($this->db->link_id))
		{
			$this->error  = 1;
			
		}
		else
		{
			$this->error = $this->db->errno();
		}
	}
	
	function login($username,$password,$remember = NULL)
	{
		if($this->check_user($username,$password) > 0)		//检验用户是否存在
		{
			if($this->need_sync)							//需要同步
			{
				$this->sync($username,$password);
			}
			$this->set_session($username);					//把用户信息存入session
			$this->set_cookie($username,$remember);			//把用户信息存入cookie
			
			return true;
		}
		else 
		{
			return false;
		}
	}
	
	
	function check_user($username,$password = null)
	{
		$post_username = $username;
		
		if($password === null)
		{
			$sql = "SELECT " . $this->field_id .
				   " FROM " . $this->table($this->user_table).
				   " WHERE " . $this->field_name . "='" . $post_username . "'";
			
			return $this->db->getOne($sql);
		}
		else 
		{
			$sql = "SELECT " . $this->field_id .
				   " FROM " . $this->table($this->user_table) .
				   " WHERE " . $this->field_name . "='" . $post_username ."' AND " . $this->field_pass . " ='" .$this->compile_password(array('password'=>$password)) . "'";
			
			return $this->db->getOne($sql);
		}
	}
	
	function add_user($username,$password,$email,$gender=-1,$bday=0,$reg_date=0,$md5password='')
	{
		if($this->check_user($username) > 0)
		{
			$this->error = ERR_USERNAME_EXISTS;
			return false;
		}
		
		$sql = "SELECT " . $this->field_id . " FROM " . $this->table($this->user_table) . " WHERE " . $this->field_email . " = '$email'";
		
		if($this->db->getOne($sql,true)>0)
		{
			$this->error = ERR_EMAIL_EXISTS;
			
			return false;
		}
		
		$post_username = $username;
		
		if($md5password)
		{
			$post_password = $this->compile_password(array('md5password'=>$md5password));
		}
		else
		{
			$post_password = $this->compile_password(array('password'=>$password));
		}
		
		$fields = array($this->field_name,$this->field_email,$this->field_pass);
		$values = array($post_username,$email,$post_password);
		
		if($gender > -1)
		{
			$fields[] = $this->field_gender;
			$values[] = $gender;
		}
		
		if($bday)
		{
			$fields[] = $this->field_bday;
			$values[] = $bday;
		}
		
		if($reg_date)
		{
			$fields[] = $this->field_reg_date;
			$values[] = $reg_date;
		}
		
		$sql = "INSERT INTO " . $this->table($this->user_table) . " (" . implode(',', $fields) .")" . " VALUES ('" . implode("','",$values). "')";
		
		$this->db->query($sql);
		
		if($this->need_sync)
		{
			$this->sync($username,$password);
		}
		
		return true;
	}
	
	
	
	
	
	function table($str)
	{
		return '`' . $this->db_name . '`.`'.$this->prefix . $str .'`';
	}
	
	function compile_password($cfg)
	{
		if(isset($cfg['password']))
		{
			$cfg['md5password'] = md5($cfg['password']);		//MD5加密
		}
		if(empty($cfg['type']))
		{
			$cfg['type'] = PWD_MD5;								//加密类型
		}
		
		
		switch($cfg['type'])
		{
			case PWD_MD5:
				return $cfg['md5password'];
				
			case PWD_PRE_SALT:
				if(empty($cfg['salt']))
				{
					$cfg['salt'] = '';
				}
				return md5($cfg['salt'] . $cfg['md5password']);

			case PWD_SUF_SALT:
				if(empty($cfg['salt']))
				{
					$cfg['salt'] = '';
				}
				
				return md5($cfg['md5password'] . $cfg['salt']);

			default:
				return '';
		}
	}
	
	
	
	function sync($username,$password='',$md5password='')
	{
		if((!empty($password)) && empty($md5password))
		{
			$md5password = md5($password);
		}
		
		$main_profile = $this->get_profile_by_name($username);
		
		if(empty($main_profile))
		{
			return false;
		}
		
		$sql = "SELECT user_name,email,password,sex,birthday".
				" FROM " . $GLOBALS['ecs']->table('users').
				" WHERE user_name = '$username'";
		$profile = $GLOBALS['db']->getRow($sql);
		
		if(empty($profile))
		{
			if(empty($md5password))
			{
				$sql = "INSERT INTO ".$GLOBALS['ecs']->table('users').
						"(user_name,email,sex,birthday,reg_time)".
						"VALUES('$username','" .$main_profile['email']."','".
						$main_profile['sex'] . "','". $main_profile['birthday'] . "','" . $main_profile['reg_time'] ."')";
			}
			else 
			{
				
			}
		}
	}
	
	function set_session($username='')
	{
		if(empty($username))		//用户名为空
		{
			$GLOBALS['sess']->destroy_session();
		}
		else 						//用户存在
		{
			$sql = "SELECT user_id,password,email FROM " . $GLOBALS['ecs']->table('users') . " WHERE user_name = '$username' LIMIT 1";
			$row = $GLOBALS['db']->getRow($sql);
			
			if($row)				//取出用户数据,存到session
			{
				$_SESSION['user_id']	= $row['user_id'];
				$_SESSION['user_name']	= $username;
				$_SESSION['email']		= $row['email'];
			}
		}
	}
	
	function set_cookie($username='',$remember= NULL)
	{
		if(empty($username))	//用户名为空则清空cookie
		{
			$time = time() - 3600;		//一个小时
			setcookie("ECS[user_id]", '', $time, $this->cookie_path);
			setcookie("ECS[password]", '', $time,$this->cookie_path);
		}
		elseif($remember)		//记住cookie
		{
			$time = time()+3600*24*15;
			
			setcookie("ECS[username]",$username,$time,$this->cookie_path,$this->cookie_domain);
			$sql = "SELECT user_id,password FROM " . $GLOBALS['ecs']->table('users') . " WHERE user_name = '$username' LIMIT 1";
			$row = $GLOBALS['db']->getRow($sql);
			if($row)
			{
				setcookie("ECS[user_id]",$row['user_id'],$time,$this->cookie_path,$this->cookie_domain);
				setcookie("ECS[password]",$row['password'],$time,$this->cookie_path,$this->cookie_domain);
			}
		}
	}
	
	function logout()
	{
		$this->set_cookie();
		$this->set_session();
	}
	
	function get_profile_by_name($username)
	{
		$post_username = $username;
		
		$sql = "SELECT " . $this->field_id . " AS user_id," . $this->field_name . " AS user_name," . 
				$this->field_email . " AS email," . $this->field_gender . " AS sex,".
				$this->field_bday . " AS birthday,". $this->field_reg_date . " AS reg_time, ".
				$this->field_pass . " AS password ".
				" FROM " .$this->table($this->user_table) .
				" WHERE " .$this->field_name . "='$post_username'";
		$row = $this->db->getRow($sql);
		
		return $row;
	}
	
	
}